<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Proxy</title>
</head>
<body>
    <script>
        const obj = {
            name: '之豪'
        }
        const newProxy2 = new Proxy(obj, {
            // 读取数据，用get
            get(target, propertyName){
                console.log('读取数据');
                return target[propertyName];
            },
            // 修改或新增数据，用set
            set(target, propertyName, value){
                console.log('修改或新增数据');
                target[propertyName] = value;
            },
            // 删除数据，用deleteProperty
            deleteProperty(target, propertyName){
                console.log('删除数据');
                return delete target[propertyName];
            }
        })
        const newProxy = new Proxy(obj, {
            // 读取数据，用get
            get(target, propertyName){
                console.log('读取数据');
                return Reflect.get(target, propertyName);
            },
            // 修改或新增数据，用set
            set(target, propertyName, value){
                console.log('修改或新增数据');
                Reflect.set(target, propertyName, value)
            },
            // 删除数据，用deleteProperty
            deleteProperty(target, propertyName){
                console.log('删除数据');
                return Reflect.deleteProperty(target, propertyName)
            }
        })
        console.log(obj); // {name: '之豪'}
        console.log(newProxy.name); // 之豪,
        newProxy.name = '花无缺'; // 修改或新增数据
        console.log(obj); // {name: '花无缺'}
        newProxy.age = 22; // 修改或新增数据
        console.log(obj); // {name: '花无缺', age: 22}
        delete newProxy.age; // 删除数据
        console.log(obj); // {name: '花无缺'}
    </script>
</body>
</html>